// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
let login = require("./Login.js");
var handy = require("./handy.js");
var Utils = require("./Utils.js");


cc.Class({
    extends: cc.Component,

    properties: {
        avatarImgNode: {
            default: null,
            type: cc.Node
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
    },

    init() {
        this.login = new login();

        this.avatarNameNode = this.node.getChildByName("self_name_layout").getChildByName("self_name");
        this.avatarGenderNode = this.node.getChildByName("self_name_layout").getChildByName("self_gender");
        this.ageNode = this.node.getChildByName("self_age");

        this.avatarNameLabel = this.avatarNameNode.getComponent(cc.Label);
        this.avatarGenderSprite = this.avatarGenderNode.getComponent(cc.Sprite);
        this.ageLabel = this.ageNode.getComponent(cc.Label);

        if(Utils.isAndroid() &&  this.login.getFlag() == "A") {
            mz.login({
                success: (res) =>{
                    console.log("获取个人信息成功 nickname = " + res.nickname + " icon ="+ res.icon);
                    this.avatarNameLabel.string = res.nickname;
                    Utils.resetFrameFromNet(this.avatarImgNode.getComponent(cc.Sprite),res.icon);
                },
                fail: (res) =>{
                    console.log(JSON.stringify(res));
                    console.log("获取个人信息失败 res = "+res);
                },
                complete: () =>{
                    console.log("login complete");
                }
            });
        }else {
            this.avatarNameLabel.string = this.login.getNickname();
            Utils.resetFrameFromNet(this.avatarImgNode.getComponent(cc.Sprite),this.login.getIcon());
        }

        
    },

    //{birth: "2000-11-18", gender: 2, icon: "https://bro-res2.flyme.cn/resources/user-icon/65.jpg", id: 476815629, name: "吃"
    onMatchSuccess(userInfo){
        console.log("onMatchSuccess userInfo.icon ="+userInfo.icon);
        // Utils.resetFrameFromNet(this.avatarImgNode.getComponent(cc.Sprite),userInfo.icon);
        // this.avatarNameLabel.string = userInfo.name;
        // if(userInfo.gender == 2) {
        //     Utils.resetFrame(this.avatarGenderSprite,"mzGameResource/female");
        // }else {
        //     Utils.resetFrame(this.avatarGenderSprite,"mzGameResource/male");
        // }
        // this.ageLabel.string = handy.getAge(userInfo.birth) + " " + handy.getAstroFromBirth(userInfo.birth);
    }
    // update (dt) {},
});
